var berserker = berserker || {}
$.extend(berserker, {
    emptyfun: function () {},
    role: function (name, parent) {

    },
    _viewList: {},
    warn: function (msg, name) {
        console.warn(name + ' : ' + msg);
    },
    log: function (msg, name) {
        console.log(name || '', msg);
    },
    getJSON: function (url, data, callback,errored) {
        var arg = arguments,
            self = arguments.callee,
            that = this;
        return berserker.xPost({
            url: url,
            cache: false,
            type: 'get',
            data:data,
            success: function (res) {
                hideInternetError();
                callback(res);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var fn = function(evt){
                    //重试的话为domConfirmReTry.data('errored')为true
                    if(!domConfirmReTry.data('errored')){
                        domConfirmReTry.data('cbk', self);
                        domConfirmReTry.data('args', arg);
                        domConfirmReTry.data('that', that);
                    }
                    InternetError(jqXHR);
                }
                //“重试”的请求
                if(domConfirmReTry.data('errored')){
                    if((new Date()) - intReLinkTimeSpan <= 1000){
                        setTimeout(fn,1000)
                    }else{
                        fn()
                    }
                    return;
                }
                //非“重试”，立即反馈
                fn();
            }
        });
    },
    getData: function (url, callback, errored) {
        var arg = arguments,
            self = arguments.callee,
            that = this;
        return berserker.xPost({
            url: url,
            cache: false,
            type: 'get',
            dataType:'text',
            success: function (res) {
                hideInternetError();
                callback(res);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var fn = function(){
                    if(!domConfirmReTry.data('errored')){
                        domConfirmReTry.data('cbk', self);
                        domConfirmReTry.data('args', arg);
                        domConfirmReTry.data('that', that);
                    }
                    //TODO根据不同的错误类型进行判断
                    InternetError(jqXHR);
                    $('#appriseMsg').html('很抱歉，网络出现问题，请确认网络畅通后重试！')
                }
                if((new Date())- intReLinkTimeSpan <= 1000){
                    setTimeout(fn,1000)
                }else{
                    fn()
                }
            }
        });
    },
    xPost: function (cfg) {
        return $.ajax({
            url: cfg.url,
            cache: false,
            data:cfg.data||{},
            dataType:cfg.dataType||'json',
            type: cfg.type||'get',
            success: function (res) {
                if(res.code == 10001){
                    hideLoading();
                    showLogin();
                    $.fallr('show', {
                        content : '<p>'+res.msg+'</p>',
                        zIndex:999999,
                        buttons:{
                            button1 : {text: '确　定', onclick: function(){
                                $.fallr('hide')
                            }}
                        }
                    })
                    return;
                }
                if(cfg.success)
                    cfg.success(res);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                if(cfg.error)
                    cfg.error(jqXHR, textStatus, errorThrown);
            }
        });
    },
})